package com.fdb.efp.console.api.rest;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.efp.console.service.facade.SRuleCollDtlParamService;
import com.fdb.efp.console.service.vo.SRuleCollDtlParamVO;
import com.fdb.efp.console.service.vo.SRuleDtlParamsVO;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SRuleCollDtlParamController extends AbstractController {

	@Autowired
	@Qualifier("sRuleCollDtlParamService")
	private SRuleCollDtlParamService sRuleCollDtlParamService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/dtl/params", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollDtlParamVO>> querySRuleCollDtlParamAll(@RequestBody SRuleCollDtlParamVO vo) {
		// 根据用户角色获取用户权限方案
		List<SRuleCollDtlParamVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRuleCollDtlParamService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRuleCollDtlParamService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRuleCollDtlParamService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sRuleCollDtlParamService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/dtl/param/{paramId}/{dtlId}", method = RequestMethod.GET)
	public @ResponseBody ResponseData<SRuleCollDtlParamVO> queryByPk(@PathVariable("paramId") String paramId,
			@PathVariable("dtlId") String dtlId) {
		SRuleCollDtlParamVO inVo = new SRuleCollDtlParamVO();
		inVo.setParamId(paramId);
		inVo.setDtlId(dtlId);
		SRuleCollDtlParamVO outputVo = sRuleCollDtlParamService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	@RequestMapping(value = "/s/rule/coll/dtl/param/dtl", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollDtlParamVO>> queryByRuleCollDtlId(@RequestBody SRuleCollDtlParamVO vo) {
		List<SRuleCollDtlParamVO> outputVo = sRuleCollDtlParamService.queryByRuleCollDtlId(vo.getDtlId());
		return getResponseData(outputVo);

	}

	/**
	 * 批量修改
	 * 
	 * @param vo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/dtl/params/dtl", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> batchUpdateParamVal(@RequestBody SRuleDtlParamsVO vo) {
		List<SRuleCollDtlParamVO> paramVOS = vo.getData();
		if (null != paramVOS && paramVOS.size() > 0) {
			for (SRuleCollDtlParamVO paramVO : paramVOS) {
				if (null != paramVO) {
					if (StringUtils.isNotEmpty(paramVO.getParamVal())) {
						BigDecimal val;
						try {
							val = new BigDecimal(paramVO.getParamVal());
							if (val.compareTo(BigDecimal.ZERO) < 0) {
								return ResponseData.create(-1, "-1", paramVO.getParamId() + "请输入一个大于等于0的数字!", false);
							}
						} catch (Exception e) {
							logger.error("转换数字异常!", e);
							return ResponseData.create(-1, "-1", paramVO.getParamId() + "请输入一个数字!", false);
						}
					}
				}
			}
		}
		int num = sRuleCollDtlParamService.batchUpdateParamVal(vo);
		return getResponseData(num);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param inSRuleCollDtlParamVo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/dtl/param", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleCollDtlParamVO inSRuleCollDtlParamVo) {
		int outputVo = sRuleCollDtlParamService.deleteByPk(inSRuleCollDtlParamVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inSRuleCollDtlParamVo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/dtl/param", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleCollDtlParamVO inSRuleCollDtlParamVo) {
		int outputVo = sRuleCollDtlParamService.updateByPk(inSRuleCollDtlParamVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inSRuleCollDtlParamVo
	 * @return
	 */
	@RequestMapping(value = "/s/rule/coll/dtl/param", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleCollDtlParam(@RequestBody SRuleCollDtlParamVO inSRuleCollDtlParamVo) {
		int outputVo = sRuleCollDtlParamService.insertSRuleCollDtlParam(inSRuleCollDtlParamVo);
		return getResponseData(outputVo);

	}
}
